<?php
/**
 * Controller to manage product images in admin area
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 * 
 */
class admin_ProductimagesController extends Bel_Controller_Aed  {

	/**
	 * Initialization. Creates left menu for index & form action
	 * Redifines _form_action, _index_action and _delete_action URLs by adding 
	 * product_id param
	 * 
	 */
	function init() {
		$this->_model = new Models_Products_Images();
		$this->_model->setProduct($this->_request->getParam('product_id'));
		$this->_formname = 'product_images_form';
		parent::init ();
		
		if($this->_request->getParam('product_id') && ($this->_request->getActionName()=='index'||$this->_request->getActionName()=='form')){
			Helpers_Products_Admin_ProductsMenu::leftMenu($this->_request->getParam('product_id'));
		}	

		$this->_form_action = $this->_form_action.'/product_id/'.$this->_request->getParam('product_id');
		$this->_index_action = $this->_index_action.'/product_id/'.$this->_request->getParam('product_id');		
		$this->_delete_action = $this->_delete_action.'/product_id/'.$this->_request->getParam('product_id');
	}

	/**
	 * Populate product_id form field vith value
	 *
	 * @param Bel_Forms_Builder $form
	 */
	protected function _prepareForm(Bel_Forms_Builder $form){
		$form->addAttributes('product_id',array('value'=>$this->_request->getParam('product_id')));
	}
	
	/**
	 * Upload product images, set is_main param to 0 if it was not defined || reset is_main field for all product images if it was
	 *
	 */
	protected function _beforeModify(){
		 $file = $this->_handleUpload();
		 if($file){
		 	$this->_request->setParam('image_filename','tmp/products/'.basename($file));
		 }
		 
		 if(!$this->_request->getParam('is_main')){
		 	$this->_request->setParam('is_main',0);
		 }else{
		 	$this->_model->resetMain($this->_request->getParam('product_id'));
		 }
	}
	
	/**
	 * Clears product cache
	 *
	 */
	protected function _afterModify(){
		Helpers_Products_Cache::clearProductCache($this->_request->getParam('product_id'));
	}
	
	/**
	 * Clears product cache, deletes image file from file system
	 *
	 */
	protected function _afterDelete(){
		$this->_afterModify();
		if($this->_current_entry){
			@unlink(HOME_DIR.$this->_current_entry['image_filename']);
		}	
	}
	
	/**
	 * Handles product image upload
	 *
	 * @return array - array of pathes to uploaded files || redirects to image add/modify form - in case of error
	 */
	private function _handleUpload(){
		$uploader = new Bel_File_Uploader();
		$uploader->setDestination(HOME_DIR.'tmp/products');
		if(!$this->_current_entry){
			$uploader->ignoreNoFile(false);
		}else{
			$uploader->ignoreNoFile(true);
		}
		$uploader->isImage();
		
		if($uploader->upload()){
			if($uploader->isReceived() && $uploader->isUploaded()){
				return $uploader->getFileName();
			}else{
				return false;
			}
		}else{
			foreach($uploader->getMessages() as $error){
				$this->_messages->setMessage($error,'error','image_filename');
			}
			
			$url = $this->_form_action;
			if($this->_request->getParam('image_id')){
				$url.='/image_id/'.$this->_request->getParam('image_id');
			}
			$this->_redirect($url);
		}		
	}
}
